package com.zmn.plat.services.impl.part;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Objects;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.google.common.collect.Lists;
import com.zmn.common.dictionary.GlobalDict;
import com.zmn.common.dto.StatusDTO;
import com.zmn.common.dto.VtDTO;
import com.zmn.common.utils.date.DateUtil;
import com.zmn.common.utils.pager.LayuiPageUtil;
import com.zmn.common.utils.pinyin.PinYinUtil;
import com.zmn.mcc.cas.model.LoginStaff;
import com.zmn.plat.manager.interfaces.cache.PartSkuCache;
import com.zmn.plat.model.dto.part.PartSkuDTO;
import com.zmn.plat.model.entity.part.PartCategory;
import com.zmn.plat.model.entity.part.PartSku;
import com.zmn.plat.model.entity.part.PartSkuQuery;
import com.zmn.plat.model.vo.part.PartSkuVO;
import com.zmn.plat.persistence.interfaces.part.PartCategoryDAO;
import com.zmn.plat.persistence.interfaces.part.PartSkuDAO;
import com.zmn.plat.services.interfaces.part.PartSkuService;

/**
 * 类描述 PartSkuServiceImpl 该类增改时应该刷新updateTime信息
 *
 * @author DaiZhichao
 * @since 2018/10/26 10:50
 */
@Service
public class PartSkuServiceImpl implements PartSkuService {
    private Logger logger = LoggerFactory.getLogger(PartSkuServiceImpl.class);

    @Resource
    private PartSkuDAO partSkuDao;
    @Resource
    private PartCategoryDAO partCategoryDAO;
    @Resource
    PartSkuCache partSkuCache;

    @Override
    public PartSku findByKey(Integer skuId) {
        return partSkuCache.getPartCategory(skuId);
    }

    @Override
    public PartSkuVO findVOByKey(Integer skuId) {
        return partSkuDao.findVOByKey(skuId);
    }

    @Override
    public List<PartSkuDTO> listPartDTOByIds(List<Integer> ids) {
        return (Objects.isNull(ids) || ids.size() == 0) ? new ArrayList<>(0) : partSkuDao.findPartDTOByIds(ids);
    }

    @Override
    public PartSkuDTO findPartDTOById(Integer skuId) {
        return partSkuDao.findPartDTOById(skuId);
    }

    @Override
    public List<PartSku> listPageByQuery(PartSkuQuery query) {
        query.setCount(this.countByQuery(query));
        return 0 == query.getCount() ? new ArrayList<>(0) : partSkuDao.listPageByQuery(new LayuiPageUtil<>(query).setStartEndTime().setLayuiPage().getQuery());
    }

    @Override
    public List<PartSkuVO> listVOPageByQuery(PartSkuQuery query) {
        query.setCount(this.countByQuery(query));
        return 0 == query.getCount() ? new ArrayList<>(0) : partSkuDao.listVOPageByQuery(new LayuiPageUtil<>(query).setStartEndTime().setLayuiPage().getQuery());
    }

    @Override
    public List<VtDTO> listByText(String text) {
        return partSkuDao.listByText(text);
    }

    @Override
    public List<VtDTO> listVtDTOByQuery(PartSkuQuery query) {
        return partSkuDao.listVtDTOByQuery(query);
    }

    @Override
    public List<PartSku> listByQuery(PartSkuQuery query) {
        return partSkuDao.listByQuery(new LayuiPageUtil<>(query).setStartEndTime().setLayuiPage().getQuery());
    }

    @Override
    public Integer countByQuery(PartSkuQuery query) {
        return partSkuDao.countByQuery(new LayuiPageUtil<>(query).setLayuiPageBlank().setStartEndTime().getQuery());
    }

    @Override
    // @CacheEvict(key = "'part:id:'+#partSku.skuId")
    public Integer updateByKey(PartSku partSku, LoginStaff staff) {
        if (Objects.nonNull(partSku.getName())) {
            this.setPinyin(partSku);
            if (Objects.nonNull(partSku.getCategId()) || Objects.nonNull(partSku.getCategOneId())) {
                this.setFullName(partSku);
            }
        }

        partSku.setUpdater(staff.getRealName());
        partSku.setUpdateTime(DateUtil.getNow());
        Integer integer = partSkuDao.updateByKey(partSku);
        //partMQService.editPartSku(partSku);
        return integer;
    }

    @Override
    // @CacheEvict(key = "'part:id:'+#skuId")
    public Integer updateStatus(Integer skuId, Integer status, LoginStaff staff) {

      return 0;
    }

    @Override
    // @CacheEvict
    public Integer updateStatusBatch(List<Integer> skuIdList, Integer status, LoginStaff staff) {

        StatusDTO dto = new StatusDTO();
        dto.setIds(skuIdList);
        dto.setStatus(status);
        dto.setUpdater(staff.getRealName());
        dto.setUpdateTime(DateUtil.getNow());
        Integer integer = partSkuDao.updateStatusBatch(dto);

        skuIdList.forEach(skuId -> {
//            if (Objects.equals(status, GlobalDict.STATUS_ENABLE)) {
//                partMQService.enablePartSku(skuId);
//            } else {
//                partMQService.disablePartSku(skuId);
//            }
        });
        return integer;
    }

    @Override
    public Integer updateSort(PartSku partSku) {
        return partSkuDao.updateSort(partSku);
    }

    @Override
    public Integer insert(PartSku partSku, LoginStaff staff) {
        this.setFullName(partSku);
        this.setPinyin(partSku);

        partSku.setSkuId(null);
        final Date now = DateUtil.getNow();
        partSku.setUpdateTime(now);
        partSku.setCreateTime(now);
        partSku.setCreater(staff.getRealName());
        partSku.setUpdater(staff.getRealName());
        Integer integer = partSkuDao.insert(partSku);
        //partMQService.pushPartSku(partSku);
        return integer;
    }

    /**
     * 描述:暂不提供删除功能
     *
     * @param skuId
     *            Integer
     * @return 数据库变更条数
     * @author DaiZhichao
     * @since 2018/12/12 15:13
     */
    private Integer deleteByKey(Integer skuId) {
      //  partMQService.removePartSku(skuId);
        return partSkuDao.deleteByKey(skuId);
    }

    private PartSku setFullName(PartSku partSku) {
        Integer categId = partSku.getCategId();
        PartCategory partCategory = partCategoryDAO.findByKey(categId);
        partSku.setFullName(partCategory.getFullName() + "-" + partSku.getName());
        return partSku;
    }

    private PartSku setPinyin(PartSku partSku) {
        String pinyinStr = PinYinUtil.converterToFirstSpell(partSku.getName());
        partSku.setPinyin(pinyinStr);
        return partSku;
    }

}
